home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / PRG / MacPerl 506 appl folder.sit / MacPerl 506 appl folder / Mac_Perl_506r1m_appl / lib / dotsh.pl < prev    next >
Text File  |  1995-03-19  |  2KB  |  71 lines

  1. #
  2. #   @(#)dotsh.pl                                               03/19/94
  3. #
  4. #   Author: Charles Collins
  5. #
  6. #   Description:
  7. #      This routine takes a shell script and 'dots' it into the current perl
  8. #      environment. This makes it possible to use existing system scripts
  9. #      to alter environment variables on the fly.
  10. #
  11. #   Usage:
  12. #      &dotsh ('ShellScript', 'DependentVariable(s)');
  13. #
  14. #         where
  15. #
  16. #      'ShellScript' is the full name of the shell script to be dotted
  17. #
  18. #      'DependentVariable(s)' is an optional list of shell variables in the
  19. #         form VARIABLE=VALUE,VARIABLE=VALUE,... that 'ShellScript' is
  20. #         dependent upon. These variables MUST be defined using shell syntax.
  21. #
  22. #   Example:
  23. #      &dotsh ('/tmp/foo', 'arg1');
  24. #      &dotsh ('/tmp/foo');
  25. #      &dotsh ('/tmp/foo arg1 ... argN');
  26. #
  27.  
  28. die "dotsh.pl doesn't work (yet) on a Mac";
  29.  
  30. sub dotsh {
  31.    local(@sh) = @_;
  32.    local($tmp,$key,$shell,*dotsh,$command,$args,$vars) = '';
  33.    $dotsh = shift(@sh);
  34.    @dotsh = split (/¥s/, $dotsh);
  35.    $command = shift (@dotsh);
  36.    $args = join (" ", @dotsh);
  37.    $vars = join ("¥n", @sh);
  38.    open (_SH_ENV, "$command") || die "Could not open $dotsh!¥n";
  39.    chop($_ = <_SH_ENV>);
  40.    $shell = "$1 -c" if ($_ =~ /^¥#¥!¥s*(¥S+(¥/sh|¥/ksh|¥/zsh|¥/csh))¥s*$/);
  41.    close (_SH_ENV);
  42.    if (!$shell) {
  43.       if ($ENV{'SHELL'} =~ /¥/sh$|¥/ksh$|¥/zsh$|¥/csh$/) {
  44.      $shell = "$ENV{'SHELL'} -c";
  45.       } else {
  46.      print "SHELL not recognized!¥nUsing /bin/sh...¥n";
  47.      $shell = "/bin/sh -c";
  48.       }
  49.    }
  50.    if (length($vars) > 0) {
  51.       system "$shell ¥"$vars;. $command $args; set > /tmp/_sh_env$$¥"";
  52.    } else {
  53.       system "$shell ¥". $command $args; set > /tmp/_sh_env$$¥"";
  54.    }
  55.  
  56.    open (_SH_ENV, "/tmp/_sh_env$$") || die "Could not open /tmp/_sh_env$$!¥n";
  57.    while (<_SH_ENV>) {
  58.        chop;
  59.        /=/;
  60.        $ENV{$`} = $';
  61.    }
  62.    close (_SH_ENV);
  63.    system "rm -f /tmp/_sh_env$$";
  64.  
  65.    foreach $key (keys(ENV)) {
  66.        $tmp .= "¥$$key = ¥$ENV{'$key'};" if $key =~ /^[A-Za-z]¥w*$/;
  67.    }
  68.    eval $tmp;
  69. }
  70. 1;
  71.